package JDBC;


import java.sql.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class MerchantRegister {
    Scanner sc =new Scanner(System.in);
    String name = null; //姓名
    String username = null; //用户名
    String password = null; // 密码
    String phone = null; //手机号
    String sex = null; // 性别
    String idNumber = null; //身份证号码
    private Connection conn = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;

    /**
     * 注册操作
     */
    public void merchantRegister(){
        conn = DBUtil.getConnection();

        Long id = new Long(getId()+ "1");
        System.out.println("=======商家注册======");
        System.out.println("请输入姓名：");
        while (true) {
            name = sc.next();
            if (name.length()>7)
                System.out.println("姓名过长，请重新输入：");
            else
                break;
        }
        System.out.println("请输入用户名：");

        while (true) {
            username = new Scanner(System.in).next();
            if(username.length()>32)
                System.out.println("用户名过长，请重新输入：");
            else{
                try {
                    String sql = "select * from employee where username = ?";
                    pstmt = conn.prepareStatement(sql);
                    pstmt.setString(1,username);
                    rs = pstmt.executeQuery();
                    if (!rs.next()){
                        DBUtil.close(conn, pstmt, rs);
                        break;
                    }
                    else
                        System.out.println("用户名重复，请重新输入：");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("请输入密码：");
        while (true) {
            password = sc.next();
            if(password.length()>32)
                System.out.println("密码过长，请重新输入：");
            else
                break;
        }
        System.out.println("请确认密码：");
        while (true) {
            String password2 = sc.next();
            if(!password2.equals(password))
                System.out.println("两次密码不一致，请重新输入：");
            else
                break;
        }
        System.out.println("请输入手机号：");
        while (true) {
            Scanner s = new Scanner(System.in);
            try {
                phone = "" + s.nextLong();
                if(phone.length()!=11)
                    System.out.println("手机号错误，请重新输入：");
                else
                    break;
            } catch (Exception e) {
                phone = null;
                System.out.println("手机号错误，请重新输入：");
            }

        }
        System.out.println("请输入性别：");
        while (true) {
            sex = sc.next();
            if(sex.equals("女") && sex.equals("男"))
                System.out.println("性别输入错误，请重新输入：");
            else
                break;
        }
        System.out.println("请输入身份证号码：");
        while (true) {
            Scanner s = new Scanner(System.in);
            idNumber = null;
            try {
                idNumber = "" + s.nextLong();

                if(idNumber.length() != 18)
                    System.out.println("身份证号输入错误，请重新输入：");
                else
                    break;
            } catch (Exception e) {
                System.out.println("身份证号码输入错误，请重新输入：");

            }

        }
        String createDataTime = getDataTime();
        String upDateTime = getDataTime();
        try {
            String sql = "INSERT INTO employee VALUES(?,?,?,?,?,?,?,1,?,?,?,?)";
            conn = DBUtil.getConnection();
            pstmt = conn.prepareStatement(sql);
            pstmt.setLong(1,id);
            pstmt.setString(2,name);
            pstmt.setString(3,username);
            pstmt.setString(4,password);
            pstmt.setString(5,phone);
            pstmt.setString(6,sex);
            pstmt.setString(7,idNumber);
            pstmt.setString(8,createDataTime);
            pstmt.setString(9,upDateTime);
            pstmt.setLong(10,id);
            pstmt.setLong(11,id);


            int i = pstmt.executeUpdate();
            if (i != 0){
                System.out.println("注册成功");
            }
            else
                System.out.println("注册失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
        DBUtil.close(conn,pstmt);



    }

    public static long getId(){
        Instant now = Instant.now();
        long second = now.getEpochSecond();
        return second;
    }

    /**
     * 获取时间信息
     */
    public static String getDataTime(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        String format = formatter.format(now);
        return format;
    }


}
